package com.xcjh.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xcjh.dto.BaseMember;
import com.xcjh.dto.yiban.ResultApi;
import com.xcjh.dto.yidui.YiduiMember;
import com.xcjh.service.YiduiService;
import com.xcjh.websocket.WebSocketSever;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class YiBanServiceImpl implements YiduiService {
    @Override
    public void save(String body) {
        ResultApi member = JSONUtil.toBean(body, ResultApi.class);
        //姓
        String name = member.getData().getResume().getChenghu().replaceAll("\\*", "");
        String id = member.getData().getIdentityAuth().getIdentityCode().replaceAll("\\*", "").substring(0, 6);
        String birthDay = member.getData().getResume().getSysBirthday();
        //生日简称
        String birthdayStr=toViewDate(member);
        //地址
        String city=getCity(id,birthDay);
        String finalCity="我是"+city+"的"+ (!city.contains("成都")?",我在成都定居了":",成都本地人")+".";
        //头像
        String head=member.getData().getHeadImg().getSmallHeadImg();

        //name+"妹妹,你是我喜欢的类型,我让介绍人牵线认识,他说你是"+birthdayStr+"的,我觉得年龄合适. 我也是"+city+"的,刚好我们是老乡."+fullCompany(member)

        String finalStr="我的"+name+"妹妹,你是我喜欢的类型,我刚刚让红娘给我们连线,她说你是"+birthdayStr+"的,我觉得年龄合适. "+finalCity+fullCompany(member);
        System.out.println(finalStr);

        BaseMember msg=new BaseMember();
        msg.setAvatar_url(head);
        msg.setViewStr(finalStr);
        WebSocketSever.sendMessageByUser("yiban", JSONUtil.toJsonStr(msg));
    }

    private String getCity(String id, String birthDay) {
        //id 510603   birthDay 1990-01-13
        String idFull = id + birthDay.replaceAll("-", "") + "6361";
        String body = HttpUtil.get("https://www.haoshudi.com/api/id/query/?userid=" + idFull);
        JSONObject result = JSONUtil.parseObj(body);
        JSONObject data = (JSONObject) result.get("data");
        String address = (String) data.get("address");
        String city = address.split(" ")[1].replace("市", "");
        String xian = address.split(" ")[2];
        System.out.println(city + xian);
        return city + xian;
    }

    private String toViewDate(ResultApi member){
        if(member.getData().getResume()==null || StrUtil.isEmpty(member.getData().getResume().getSysBirthday())){
            return null;
        }
        String birthday=member.getData().getResume().getSysBirthday();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfView=new SimpleDateFormat("yy年MM月dd");
        try {
            Date date = sdf.parse(birthday);
            return sdfView.format(date);
        } catch (ParseException e) {
            System.out.println("解析:"+birthday);
            throw new RuntimeException(e);
        }
    }
    private String fullCompany(ResultApi member){
        if(member.getData().getAuthJob()!=null && StrUtil.isNotEmpty(member.getData().getAuthJob().getCompName())){
            String company=member.getData().getAuthJob().getCompName();
            company=company.replace("成都市","")
                    .replace("成都","")
                    .replace("四川省","")
                    .replace("四川","")
                    .replace("有限公司","")
                    .replace("股份","")
            ;
            return "我之前也在"+company+"干过,所以我觉得是巧合也是缘分";
        }else {
            return "";
        }
    }
}
